package com.study.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.app.domain.AppArticle;
import com.study.app.mapper.AppArticleMapper;
import com.study.app.service.IAppArticleService;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.app.domain.bo.AppArticleBo;
import com.study.app.domain.vo.AppArticleVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * App车辆指南Service业务层处理
 *
 * @author study
 * @date 2023-09-08
 */
@RequiredArgsConstructor
@Service
public class AppArticleServiceImpl implements IAppArticleService {

    private final AppArticleMapper baseMapper;

    /**
     * 查询App车辆指南
     */
    @Override
    public AppArticleVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询App车辆指南列表
     */
    @Override
    public TableDataInfo<AppArticleVo> queryPageList(AppArticleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppArticle> lqw = buildQueryWrapper(bo);
        Page<AppArticleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询App车辆指南列表
     */
    @Override
    public List<AppArticleVo> queryList(AppArticleBo bo) {
        LambdaQueryWrapper<AppArticle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppArticle> buildQueryWrapper(AppArticleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppArticle> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleType()), AppArticle::getVehicleType, bo.getVehicleType());
        lqw.eq(bo.getArticleType() != null, AppArticle::getArticleType, bo.getArticleType());
        lqw.eq(StringUtils.isNotBlank(bo.getArticleTitle()), AppArticle::getArticleTitle, bo.getArticleTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getArticleImages()), AppArticle::getArticleImages, bo.getArticleImages());
        lqw.eq(StringUtils.isNotBlank(bo.getArticleContent()), AppArticle::getArticleContent, bo.getArticleContent());
        lqw.eq(bo.getArticleStatus() != null, AppArticle::getArticleStatus, bo.getArticleStatus());
        lqw.eq(bo.getViewCount() != null, AppArticle::getViewCount, bo.getViewCount());
        lqw.eq(bo.getLikeCount() != null, AppArticle::getLikeCount, bo.getLikeCount());
        lqw.eq(bo.getCommentCount() != null, AppArticle::getCommentCount, bo.getCommentCount());
        lqw.eq(bo.getTop() != null, AppArticle::getTop, bo.getTop());
        lqw.eq(bo.getArticleCategory() != null, AppArticle::getArticleCategory, bo.getArticleCategory());
        return lqw;
    }

    /**
     * 新增App车辆指南
     */
    @Override
    public Boolean insertByBo(AppArticleBo bo) {
        AppArticle add = BeanUtil.toBean(bo, AppArticle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改App车辆指南
     */
    @Override
    public Boolean updateByBo(AppArticleBo bo) {
        AppArticle update = BeanUtil.toBean(bo, AppArticle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppArticle entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除App车辆指南
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}